package com.sky.mapper.courier;

import com.sky.dto.CourierTradeQueryDto;
import com.sky.pojo.CourierTradeOrder;
import com.sky.vo.CourierTradeOrderVO;
import com.sky.vo.OrderDetailVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface CourierTradeOrderMapper {
    List<Long> findLeastOrdersThreeCouriers(List<Long> couriersIds, LocalDateTime today);


    void save(CourierTradeOrder tradeOrder);

    @Select("select * from tb_courier_trade_order where waybill_status = #{status} and courier_id = #{courierId} ")
    List<CourierTradeOrder> findByCourierAndStatus(Integer status, Integer courierId);

    @Select("select * from tb_courier_trade_order where trade_no = #{tradeNo} ")
    CourierTradeOrder findTradeOrderByTradeNo(Long tradeNo);

    @Update("update tb_courier_trade_order set waybill_status = #{status} where trade_no = #{tradeNo}")
    void updateStatusByTradeNo(Long tradeNo, Integer status);

    @Update("update tb_courier_trade_order set waybill_status = #{status} ,trade_complete_time = #{completeTime} where trade_no = #{tradeNo}")
    void updateCompleteStatusByTradeNo(Long tradeNo, Integer status,LocalDateTime completeTime);

/*    @Select("select od.* from order_detail od where order_id in" +
            "(select o.id from tb_courier_trade_order tcto " +
            "left join orders o on tcto.order_number = o.`number`" +
            "where tcto.trade_no = #{tradeNo})")*/
    @Select("select od.* from order_detail od " +
            "left join orders o on od.order_id = o.id " +
            "left join tb_courier_trade_order tcto on tcto.order_number = o.number " +
            "where tcto.trade_no = #{tradeNo}")
    List<OrderDetailVO> confirmPickUpList(Long tradeNo);

    @Update("update tb_courier_trade_order set courier_id = #{courierId} where trade_no = #{tradeNo}")
    void updateCourierByTradeNo(Long tradeNo, Long courierId);

//    List<CourierTradeOrderVO> queryWaybillsByDateD(CourierTradeQueryDto dto);

    List<CourierTradeOrder> selectCourierOrders(String date, Integer status, Long courierId);

    Double ordersCompleteAvgTime(Long courierId, LocalDate firstDay, LocalDate lastDay);

    Long ordersCountByCourierIdAndDate(Long courierId, LocalDate firstDay, LocalDate lastDay, Integer status);

    Map<String, Long> ordersCountByDateAndStatus(Long courierId,LocalDate firstDay, LocalDate lastDay, List<String> dateList, Integer status);
}
